<template>
    <div style="width: 500px">
        <el-input
                style="margin-bottom: 8px"
                prefix-icon = "el-icon-search"
                placeholder="请输入部门名称"
                v-model="filterText">
        </el-input>

        <el-tree
                :data="deps"
                :props="defaultProps"
                :filter-node-method="filterNode"
                ref="tree">
             <span class="custom-tree-node" style="display: flex;justify-content: space-between;width: 100%" slot-scope="{ node, data }">
                <span>{{ node.label }}</span>
                <span >
                  <el-button
                          class="depBtn"
                          type="primary"
                          size="mini"
                          @click="() => addDep(data)">
                    添加部门
                  </el-button>
                  <el-button
                          class="depBtn"
                          type="danger"
                          size="mini"
                          @click="() => removeDep(data)">
                    删除部门
                  </el-button>
                </span>
             </span>
        </el-tree>
        <el-dialog
                title="添加部门"
                :visible.sync="dialogVisible"
                width="30%">
            <div>
                <table>
                    <tr>
                        <td><el-tag type="success" effect="dark">上级部门</el-tag></td>
                        <td>{{pname}}</td>
                    </tr>
                    <tr>
                        <td><el-tag type="success" effect="dark" style="margin-top: 8px">部门名称</el-tag></td>
                        <td><el-input v-model="dep.name" placeholder="请输入部门名称" style="width: 280px;padding: 2px"></el-input></td>
                    </tr>
                </table>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button size="medium" @click="dialogVisible = false">取 消</el-button>
                <el-button size="medium" type="primary" @click="updateButton">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: "DepMana",
        watch: {
            filterText(val) {
                this.$refs.tree.filter(val);
            }
        },
        data(){
            return {
                pname:"",
                dialogVisible:false,
                filterText:"",
                deps:[

                ],
                dep:{
                    name:"",
                    parentId:-1
                },
                defaultProps: {
                    children: 'children',
                    label: 'name'
                }
            }
        },
        mounted() {
            this.initDep();
        },
        methods: {
            addDep2Deps(deps,dep){
                for(let i=0;i<deps.length;i++){
                    let d = deps[i];
                    if(d.id == dep.parentId){
                        d.children = d.children.concat(dep);
                    }else{
                        this.addDep2Deps(d.children,dep);
                    }
                }
            },
            updateButton(){
              this.postRequest("/system/basic/department/",this.dep).then(resp=>{
                  if(resp){
                      this.addDep2Deps(this.deps,resp.obj);
                      this.dialogVisible=false;
                      this.initDepParam();
                  }
              })
            },
            initDepParam(){
                this.dep ={
                    name:"",
                        parentId:-1
                };
                this.pname = "";
            },
            addDep(data){
                this.pname = data.name;
                this.dep.parentId = data.id;
                this.dialogVisible=true;
            },
            removeDepFormDeps(deps,id){
                for(let i=0;i<deps.length;i++){
                    let dep = deps[i];
                    if(dep.id == id){
                        deps.splice(i,1);
                    }else{
                        this.removeDepFormDeps(dep.children,id);
                    }
                }
            },
            removeDep(data){
                if(data.isParent){
                    this.$message.error("父部门不可以直接删除")
                }else{
                    this.$confirm('此操作将删除【'+data.name+'】部门是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        this.deleteRequest("/system/basic/department/"+data.id).then(resp=>{
                            if(resp){
                                this.removeDepFormDeps(this.deps,data.id);
                            }
                        })
                    }).catch(() => {
                        this.$message({
                            type: 'info',
                            message: '已取消删除'
                        });
                    });
                }
            },
            filterNode(value, data) {
                if (!value) return true;
                return data.name.indexOf(value) !== -1;
            },
            initDep(){
                this.getRequest("/system/basic/department/").then(resp=>{
                    if(resp){
                        this.deps = resp;
                    }
                })
            }
        }
    }
</script>

<style>
    .depBtn {
        padding: 2px;
    }

</style>